package com.kupurui.asteacher.ui.index;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.facebook.drawee.view.SimpleDraweeView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.ClassDatailsAdapter;
import com.kupurui.asteacher.bean.ClassDatailsInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.util.PtrInitHelper;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 班级详情
 * Created by Administrator on 2017/7/18.
 */

public class ClassDetailsAty extends BaseAty implements PtrHandler, LoadMoreHandler {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.imgv_class_icon)
    SimpleDraweeView imgvClassIcon;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.tv_empty)
    TextView tvEmpty;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;
    @Bind(R.id.tv_school_name)
    TextView tvSchoolName;
    @Bind(R.id.tv_grade)
    TextView tvGrade;
    @Bind(R.id.tv_class_name)
    TextView tvClassName;
    @Bind(R.id.tv_stu_num)
    TextView tvStuNum;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.tv_work_refer)
    TextView tvWorkRefer;
    @Bind(R.id.tv_exam_correct)
    TextView tvExamCorrect;
    @Bind(R.id.tv_work_correct)
    TextView tvWorkCorrect;

    ClassDatailsInfo classDatailsInfo;
    List<ClassDatailsInfo.StudentListBean> list;
    ClassDatailsAdapter adapter;

    private String class_id = "";
    private String name = "";
    private int page = 1;

    @Override
    public int getLayoutId() {
        return R.layout.class_details_aty;
    }

    @Override
    public void initData() {
        if (getIntent().getExtras()!=null){
            class_id = getIntent().getStringExtra("class_id");
        }
        PtrInitHelper.initPtr(this, ptrFrame);
        ptrFrame.setPtrHandler(this);
        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(true);
        loadMoreListViewContainer.loadMoreFinish(false, true);
        loadMoreListViewContainer.setLoadMoreHandler(this);
        listview.setFooterDividersEnabled(false);
        tvEmpty.setText("没有这个学生...");
        listview.setEmptyView(llEmpty);

        list = new ArrayList<>();
        adapter = new ClassDatailsAdapter(this, list, R.layout.class_details_item);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("class_id",class_id);
                bundle.putString("student_id",list.get(position).getSt_id());
                startActivity(StudentDatailsAty.class, bundle);
            }
        });

        etName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    /*隐藏软键盘*/
                    InputMethodManager imm = (InputMethodManager) v
                            .getContext().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    name = etName.getText().toString();
                    page = 1;
                    new Teacher().studentList(class_id,name,page+"",ClassDetailsAty.this,0);
                }
                return false;
            }
        });
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Teacher().studentList(class_id,name,page+"",this,0);
    }

    @OnClick({R.id.iv_back})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                classDatailsInfo = AppJsonUtil.getObject(result,ClassDatailsInfo.class);
                tvSchoolName.setText("学校："+classDatailsInfo.getClass_info().getSchool());
                tvGrade.setText("年级："+classDatailsInfo.getClass_info().getGrade());
                tvClassName.setText("班级："+classDatailsInfo.getClass_info().getClass_name());
                tvStuNum.setText("班级人数："+classDatailsInfo.getClass_info().getClass_num()+"人");
                tvWorkRefer.setText(classDatailsInfo.getClass_info().getZuoye_tijiaolv()+"%");
                tvExamCorrect.setText(classDatailsInfo.getClass_info().getKaoshi_zhengquelv()+"%");
                tvWorkCorrect.setText(classDatailsInfo.getClass_info().getZuoye_zhengquelv()+"%");
                page = 1;
                list.clear();
                list.addAll(classDatailsInfo.getStudent_list());
                adapter.notifyDataSetChanged();
                ptrFrame.refreshComplete();
                loadMoreListViewContainer.loadMoreFinish(false, true);
                break;
            case 1:
                classDatailsInfo = AppJsonUtil.getObject(result,ClassDatailsInfo.class);
                if (Toolkit.listIsEmpty(classDatailsInfo.getStudent_list())){
                    loadMoreListViewContainer.loadMoreFinish(false, false);
                }else{
                    page++;
                    list.addAll(classDatailsInfo.getStudent_list());
                    adapter.notifyDataSetChanged();
                    loadMoreListViewContainer.loadMoreFinish(false, true);
                }
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return false;
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        new Teacher().studentList(class_id,name,page+"",this,0);
    }

    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        new Teacher().studentList(class_id,name,(page+1)+"",this,1);
    }
}
